package com.qfedu.mall.service.Impl;

import com.qfedu.mall.beans.entity.Order;
import com.qfedu.mall.beans.entity.ShoppingCart;
import com.qfedu.mall.beans.vo.ShoppingCartVO;
import com.qfedu.mall.common.vo.ResStatus;
import com.qfedu.mall.common.vo.ResultVO;
import com.qfedu.mall.mapper.ShoppingCartMapper;
import com.qfedu.mall.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper mapper;

    @Override
    public ResultVO addShoppingCart(ShoppingCart cart) {

        int insert = mapper.insert(cart);
        if (insert>0){
            return new ResultVO(ResStatus.OK,"添加成功",null);
        }else{
            return new ResultVO(ResStatus.NO,"添加失败",null);
        }
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVO listShoppingCartsByUserId(int userId) {
        List<ShoppingCartVO> listvos=  mapper.selectShopcartByUserId(userId);

        if (listvos.size() == 0){
            return new ResultVO(ResStatus.NO,"没有查到购物车信息",null);
        }else{
            return new ResultVO(ResStatus.OK,"查询成功",listvos);
        }
    }

    @Override
    public ResultVO updateCartNum(int cartId, int cartNum) {
        int i = mapper.updateCartnumByCartid(cartId, cartNum);
        if (i>0){
            return new ResultVO(ResStatus.OK,"修改成功",null);
        }else {
            return new ResultVO(ResStatus.NO,"修改失败",null);
        }
    }

    @Override
    public ResultVO listShoppingCartByCids(String cids) {
        String[] arr = cids.split(",");
        List<Integer> scid = new ArrayList<>();
        for (String s : arr) {
            scid.add(Integer.parseInt(s));
        }

        List<ShoppingCartVO> vos = mapper.selectShopcartByCids(scid);

        return new ResultVO(ResStatus.OK,"success",vos);


    }

    @Override
    public ResultVO deleteCartbyCids(String cids) {

        String[] arr = cids.split(",");
        List<Integer> cidsList = new ArrayList<>();
        for (String s : arr) {
            cidsList.add(Integer.parseInt(s));
        }
        for (int cid : cidsList){
            mapper.deleteByPrimaryKey(cid);
        }
        return new ResultVO(ResStatus.OK,"删除成功","");
    }

    @Override
    public ResultVO settlementCart(Integer cartId) {
        Order order = new Order();

//        ResultVO resultVO = new ResultVO(ResStatus.OK,"Success",);
        return null;
    }
}
